//加载字典
function offlineTypeDict(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.offline_type;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
//加载字典
function jclxTypeDict(value, options, row){
    //sex为自定义字典项
    var statusObj = vm.jclx;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
function formatOption(value,options,rowObject) {
    let id = rowObject.id;
    let studytype=rowObject.studytype;
    return "<font color='blue'><a href='#' onclick='print("+id+","+studytype+")'>打印</a></font>"
}

function print(id,studytype){
    if(studytype == '1') {
        //先关闭菜单实现刷新效果
        parent.closeTabByPageId("10012");
        //id为菜单ID，请从菜单管理中查找，如非配置的菜单则设定个比较大的数为ID，以免与系统菜单冲突。
        parent.addTabs({
            id: '10012',
            title: '打印',
            close: true,
            url: 'modules/offlinestudy/SixStudyPrint.html?id=' + id,
            urlType: 'relative'
        });
    }else{
        //先关闭菜单实现刷新效果
        parent.closeTabByPageId("10003");
        //id为菜单ID，请从菜单管理中查找，如非配置的菜单则设定个比较大的数为ID，以免与系统菜单冲突。
        parent.addTabs({id:'10003',title: '打印',close: true,url: 'modules/offlinestudy/ThreeAndThreeStudy.html?id='+id,urlType: 'relative'});
    }
}

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'offlinestudy/subscribedate/subscribeList',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden :true},
			{ label: '', name: 'userid', index: 'userid', width: 80,hidden:true },
			{ label: '', name: 'subscribedateId', index: 'subscribedate_id', width: 80,hidden:true },
			{ label: '学习类型', name: 'studytype', index: 'studytype', width: 60,formatter:offlineTypeDict },
			{ label: '', name: 'subscribetype', index: 'subscribetype', width: 80,hidden:true },
			{ label: '', name: 'address', index: 'address', width: 80 ,hidden:true},
			{ label: '姓名', name: 'name', index: 'name', width: 80 },
			{ label: '身份证号', name: 'sfzh', index: 'sfzh', width: 120 },
			{ label: '档案编号', name: 'dabh', index: 'dabh', width: 80 },
			{ label: '驾车类型', name: 'jclx', index: 'jclx', width: 80,formatter:jclxTypeDict },
			{ label: '电话', name: 'tel', index: 'tel', width: 80 },
			{ label: '', name: 'fj', index: 'fj', width: 80,hidden:true },
			{ label: '录入时间', name: 'ldate', index: 'ldate', width: 80,formatter:formatSubscribeTime },
			{ label: '', name: 'subscribedateTypeid', index: 'subscribedate_typeid', width: 80 ,hidden:true},
			{ label: '学习开始日期', name: 'startdate', index: 'startdate', width: 80,formatter:formatDate },
			{ label: '学习结束日期', name: 'enddate', index: 'enddate', width: 80,formatter:formatDate},
            { label: '操作', name:'', index:'', width:80,formatter:formatOption}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		subscribe: {},
        offline_type:[],
        subscribedate:[],
        queryCondition:{"studytype":''}
	},
    mounted:function(){
        this.dateDefault();
    },
    created:function() {
        $.get(baseURL+"/sys/dict/select/offline_type",function(r){
            if(r.code == 0) {
                vm.offline_type = r.dict.offline_type;
            }else{
                console.log(r.msg);
            }
        });
        $.get(baseURL+"/sys/dict/select/jclx",function(r){
            if(r.code == 0) {
                vm.jclx = r.dict.jclx;
            }else{
                console.log(r.msg);
            }
        });
    },
	methods: {
		query: function () {
			vm.reload();
		},
		getInfo: function(id){
			$.get(baseURL + "offlinestudy/subscribedate/subscribeInfo/"+id, function(r){
                vm.subscribe = r.subscribe;
                vm.varSubscribedate = vm.subscribe.subscribedate;
                vm.varJclx=vm.subscribe.jclx;
                vm.varStudytype = vm.subscribe.studytype;
                vm.varSubscribedate = formatDate(vm.subscribe.startdate)+"至"+formatDate(vm.subscribe.enddate);
            });
		},
		reload: function (event) {
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
			vm.showList = true;
            postData = {
                "name":vm.queryCondition.name==null?undefined:vm.queryCondition.name,
                "sfzh":vm.queryCondition.sfzh==null?undefined:vm.queryCondition.sfzh,
                "studytype":vm.queryCondition.studytype==null?undefined:vm.queryCondition.studytype,
                "startdate":$("#datepicker_start").val()==''?undefined:$("#datepicker_start").val(),
                "enddate":$("#datepicker_end").val()==''?undefined:$("#datepicker_end").val()
            };
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                page:page,
                postData:postData
            }).trigger("reloadGrid");
		},
        dateDefault: function(){
            //初始化日期范围组件
            $('#datepicker_range input').datepicker({
                language: 'zh-CN',//显示中文
                startView: 0, //选择器开始界面 0：天（默认 1：月2：年3：十年 4：世纪
                minViewMode: 0,//最小选择精度 0：天（最小默认）
                maxViewMode: 2, //最大选择精度 4：世纪（最大默认）
                format: 'yyyy-mm-dd',//显示格式
                minView: "day",//设置只显示到月份
                // initialDate: new Date(),
                autoclose: true,//选中自动关闭
                // todayBtn: true//显示今日按钮
                clearBtn: true //清除按钮
                //todayHighlight: true //今日高亮
            });
        },
        exportExcel:function(){
            document.forms[0].action=baseURL+"/offlinestudy/subscribedate/exceportExcel";
            document.forms[0].submit();
        }
	}
});

function formatDate(cellValue) {
    var dateStr = "";
    if(cellValue){
        var crtTime = new Date(cellValue);
        dateStr = dateFtt("yyyy-MM-dd",crtTime);
    }
    return dateStr;
}